/*
 * @Author: _LJP 2634716776@qq.com
 * @Date: 2024-01-07 15:49:08
 * @LastEditors: _LJP 2634716776@qq.com
 * @LastEditTime: 2024-01-09 15:49:22
 * @FilePath: /my_-linux/Pro24/Tree/Map_Set/Map.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include"RBTree.hpp"

enum COLOR;

namespace MYSTL{
    template<class K,class V>
    class Map{
        struct mapKeyOfT{
            const K &operator()(const std::pair<const K, V> &kv)
            {
                return kv.first;
            }
        };
    public:
        typedef typename RBTree<K, std::pair<const K, V>, mapKeyOfT>::iterator iterator;
        // typedef类模板的时候需要加上typename ,否则编译器不知道typedef的是类型还是成员变量
       
        pair<iterator, bool> Insert(const std::pair<const K, V> &kv) {
          pair<iterator,bool> ret = _rbt.Insert(kv);
          return ret;
        }

        iterator begin(){
            return _rbt.begin();
        }

        iterator end(){
            return _rbt.end();
        }

        V& operator[](const K& key){
            std::pair<iterator,bool> ret = _rbt.Insert(std::make_pair(key,V()));
            return ret.first->second;
        }

    protected:

    private:
        RBTree<K , std::pair<const K,V> , mapKeyOfT> _rbt;
        //模板参数传入两个参数分别为K类型的数据与pair
    };    

    void Maptest1(){
        Map<int,int> m1;
        m1.Insert(std::make_pair(1, 1));
        m1.Insert(std::make_pair(3, 3));
        m1.Insert(std::make_pair(2, 2));
    }
    
    void Maptest2(){
    int a[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
    Map<int,int> m;
        for (auto e : a) { 
            m.Insert(std::make_pair(e, e)); 
        }

        Map<int,int>::iterator it = m.begin();
        while(it!=m.end()){
            cout<<it->first<<" : "<<it->second<<endl;
            ++it;
        }
    }

    void Maptest3() {
      int a[] = {16, 3, 7, 11, 9, 26, 18, 14, 15};
      Map<int, int> m;
      for (auto e : a) {
        m[e]++;
      }

      Map<int, int>::iterator it = m.begin();
      while (it != m.end()) {
        cout << it->first << " : " << it->second << endl;
        ++it;
      }
    }
}